package com.lin.sqltool.utils;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Alert;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.util.List;

public class CommonUtil {

    /**
     * 显示弹窗
     * @param type
     * @param title
     * @param content
     */
    public static void showAlert(Alert.AlertType type, String title, String content) {
        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setContentText(content);
        alert.showAndWait();
    }

    /**
     * 设置表格内容
     * @param tableView
     * @param data
     */
    public static void setTableData(TableView<List<String>> tableView , List<List<String>> data) {
        if (!data.isEmpty()){
            List<String> headers = data.getFirst(); //表头信息
            for (int i = 0; i < headers.size(); i++) {
                TableColumn<List<String>, String> column = new TableColumn<>(headers.get(i));
                final int colIndex = i;
//                column.setCellValueFactory(new PropertyValueFactory<>(""));
                column.setCellValueFactory(cellData -> {
                    List<String> row = cellData.getValue();
                    if (row.size() > colIndex) {
                        return javafx.beans.binding.Bindings.createStringBinding(() -> row.get(colIndex));
                    }
                    return javafx.beans.binding.Bindings.createStringBinding(() -> "");
                });
                tableView.getColumns().add(column);
            }
            ObservableList<List<String>> observableData = FXCollections.observableArrayList(data.subList(1, data.size()));
            tableView.setItems(observableData);
        }
    }


}
